import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpSentEvent, HttpHeaderResponse, HttpProgressEvent, HttpResponse, HttpUserEvent } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';

@Injectable({
  providedIn: 'root'
})
export class HttpInterceptorService implements HttpInterceptor {


  constructor() { }
  base_url: string = "http://localhost:8080";
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
    if (req.params.get('mock') === 'true') {
      const apiReq = req.clone({
        url: `${req.url}`,
        withCredentials: true
      });
      return next.handle(apiReq);
    } else {
      const apiReq = req.clone({ url: `${this.base_url}/${req.url}` });
      return next.handle(apiReq);
    }
  }

}
